//
//  HR_BLELinkView.m
//  Pods
//
//  Created by xiaoming on 17/07/26.
//
//

#import "HR_BLELinkView.h"
#import <CoreBluetooth/CoreBluetooth.h>
#import "HR_BLELinkConstant.h"
#import "HR_BLELinkTableViewCell.h"

static NSString *const cellIdentifier = @"HR_BLELinkTableViewCell";

@interface HR_BLELinkView ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tbView;
/** 菊花转动 */
@property (assign, nonatomic) BOOL isAnimatingOfCell;

@end

@implementation HR_BLELinkView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupSubviews];
}

#pragma mark - ---------- Private ----------

- (void)setupSubviews {
    [self setupTableView];
}

- (void)setupTableView {
    [self.tbView registerNib:[UINib nibWithNibName:NSStringFromClass([HR_BLELinkTableViewCell class]) bundle:[NSBundle bundleForClass:[HR_BLELinkTableViewCell class]]] forCellReuseIdentifier:cellIdentifier];
}

/**
 获取当前点击的下标蓝牙外设

 @param indexPath 下标
 @return 蓝牙外设
 */
- (CBPeripheral *)elicitPeripheralWithSelectedIndexPath:(NSIndexPath *)indexPath {
    CBPeripheral *peripheral = [self.dataSource objectAtIndex:indexPath.row];
    return peripheral;
}

/**
 获取当前蓝牙外设在数据源中下标

 @param peripheral 蓝牙外设
 @return 下标
 */
- (NSInteger)elicitCurrentIndexWithCBPeripheral:(CBPeripheral *)peripheral {
    __block NSInteger currentIndex = -1;
    if (![self.dataSource containsObject:peripheral]) {
        NSLog(@"\n 外设'%@'已扫描不到...", peripheral.name);
        return currentIndex;
    }
    [self.dataSource enumerateObjectsUsingBlock:^(CBPeripheral * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isEqual:peripheral]) {
            currentIndex = idx;
            *stop = YES;
        }
    }];
    return currentIndex;
}

#pragma mark - ---------- Public ----------
- (void)reloadData {
    [self.tbView reloadData];
}

- (void)indicatorAnimating:(BOOL)animating peripheral:(CBPeripheral *)peripheral {
    WEAK(weakSelf);
    [self.dataSource enumerateObjectsUsingBlock:^(CBPeripheral * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //先关闭所有动画，避免数据源插入新蓝牙外设，下标位置变化造成多菊花同时显示
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
        HR_BLELinkTableViewCell *cell = (HR_BLELinkTableViewCell *)[weakSelf.tbView cellForRowAtIndexPath:indexPath];
        [cell activityIndicatorAnimating:NO];
        weakSelf.isAnimatingOfCell = NO;
    }];
    
    NSInteger index = [self elicitCurrentIndexWithCBPeripheral:peripheral];
    if (index < 0) {
        return;
    }
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    HR_BLELinkTableViewCell *cell = (HR_BLELinkTableViewCell *)[self.tbView cellForRowAtIndexPath:indexPath];
    [cell activityIndicatorAnimating:animating];
    weakSelf.isAnimatingOfCell = animating;
}

#pragma mark - ---------- Event action ----------
- (IBAction)cancelAction:(UIButton *)sender {
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

#pragma mark - ---------- TableView Delegate && DataSource ----------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return .1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return .1f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HR_BLELinkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    CBPeripheral *peripheral = self.dataSource[indexPath.row];
    cell.peripheral = peripheral;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.isAnimatingOfCell) {
        return;
    }
    
    CBPeripheral *peripheral = [self elicitPeripheralWithSelectedIndexPath:indexPath];
    if (self.selectPrepheralBlock) {
        self.selectPrepheralBlock(peripheral.identifier);
    }
}

@end
